package com.bw.aop;

import com.bw.domain.BResult;
import com.bw.domain.User;
import com.bw.mapper.UserMapper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

//@Component
//@Aspect
public class LoginAOP {

    @Autowired
    HttpServletRequest request;

    @Autowired
    UserMapper userMapper;


    @Around("@annotation(com.bw.annotation.LogAnno)")
    public Object aop(ProceedingJoinPoint joinPoint) throws Throwable {

        User user = (User) joinPoint.getArgs()[0];
        String name = user.getName();  // 登录用户名

        String host = request.getRemoteHost();

        BResult proceed = (BResult) joinPoint.proceed();
        try {
            int code = proceed.getCode();  // code 1  登录成功    0登录失败
//            userMapper.addLog(name,host, code);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return proceed;
    }
}
